var signUpButton = document.getElementById('signUp')
var signInButton = document.getElementById('signIn')
var container = document.getElementById('dowebok')

signUpButton.addEventListener('click', function () {
    container.classList.add('right-panel-active')
})

signInButton.addEventListener('click', function () {
    container.classList.remove('right-panel-active')
});

// 登录
function login() {
    let account = document.getElementsByName("account")[0].value.replace(/\s*/g, "");
    let password = document.getElementsByName("password")[0].value.replace(/\s*/g, "");

    if (account == "") {
        $("#account-login").css("display", "").css("color", "red");
        return;
    } else {
        $("#account-login").css("display", "none");
    }
    if (password == "") {
        $("#password-login").css("display", "").css("color", "red");
        return;
    } else {
        $("#password-login").css("display", "none");
    }
    $.post(serverConfig.serverUrl + `/devs/user/login/${account}/${password}`, function (result) {
        if (result.code == 404) {
            $("#error-login").css("display", "").css("color", "red");
            return;
        }
        if (result.code == 200) {
            $("#error-login").css("display", "none");
            //将token放入到本地存储中
            console.log(result.data)
            localStorage.setItem("token", result.data);
            window.location = '../html/index.html';
        }
    })
}

//注册
$(function () {
    $("#register-account")[0].onblur = function () {
        let account = $(this).val()

        let span = $("#register-account-span");

        let reg = /^[a-z]+$/
        if (reg.test(account)) {
            let flag = false;

            $.ajax({
                type: 'get',
                url: serverConfig.serverUrl + `/devs/user/${account}`,
                async: false,
                success: function (result) {
                    if (result.code == 200) {
                        span.css("display", "none")
                        flag = true;
                    } else {
                        span.css("display", "").css("color", "red")
                        span[0].innerText = "用户名重复";
                        flag = false
                    }
                }
            })
            return flag;
        } else {
            span.css("display", "").css("color", "red")[0].innerText = "用户名只能由字母组成"
            return false;
        }
    }

    $("#register-password")[0].onblur = function () {
        let value = $(this).val();
        let reg = /^[a-z]+$/
        let span = $("#register-password-span");
        if (reg.test(value)) {
            span.css("display", "none");
            return true;
        } else {
            span.css("display", "").css("color", "red")[0].innerText = "密码必须由字母组成"
            return false;
        }
    }

    $("#register-pwd")[0].onblur = function () {
        let pwd2Value = $(this).val();
        let pv = $("#register-password").val()
        let span = $("#register-pwd-span");
        if (pwd2Value != "" && pwd2Value == pv) {
            span.css("display", "none");
            return true;
        } else {
            span.css("display", "").css("color", "red")[0].innerText = "两次密码不一致";
            return false;
        }
    }
})
//注册按钮
function register () {

    let uname = $("#register-account");
    let pwd = $("#register-password");
    let pwd2 = $("#register-pwd");

    if (uname[0].onblur() & pwd[0].onblur() & pwd2[0].onblur()) {
        $.post(serverConfig.serverUrl +'/devs/user/register', {account: uname.val(), password: pwd.val()}, function (result) {
            if (result.code == 200) {
                layer.msg('恭喜你,注册成功了', {icon: 6})
                setTimeout(function () {
                    window.location = '../html/login.html';
                }, 1500)
            } else {
                layer.msg('注册失败', {icon: 5})
            }
        })
    }
}
function phoneLogin(){
    document.getElementById("idpassword").style= "display:none"
    document.getElementById("phone").style = "display:flex"
}
function userLogin(){
    document.getElementById("phone").style = "display:none"
    document.getElementById("idpassword").style= "display:flex"
}


phoneNum.addEventListener('blur',function (){

})
function send(){
    let zz = /^1\d{10}$/
    let phone = document.getElementsByName("phone")[0].value
    if(!zz.test(phone)){
        layer.msg("请正确填写手机号！")
    }else {
        let count = document.getElementById("y-send"); //获取验证码按钮
        let time = 59;
        // 注册单击事件
        // 禁用按钮
        count.disabled = true;
        // 开启定时器
        let timer = setInterval(function() {
            // 判断剩余秒数
            if (time == 0) {
                // 清除定时器和复原按钮
                clearInterval(timer);
                count.disabled = false;
                count.innerHTML = '获取验证码';
            } else {
                count.innerHTML = `${time}秒后重新获取`;
                time--;
            }
        }, 1000);
        $.ajax({
            type: 'get',
            url:serverConfig.serverUrl + `/devs/user/phone/${phone}`,
            success:function (result){
                if(result.code == 200){
                    layer.msg("发送成功！")
                }else {
                    layer.msg("发送失败！")
                }
            }
        })
    }
}

function loginPhone(){
    $.ajax({
        type:'get',
        url:serverConfig.serverUrl + `/devs/user/phoneLogin`,
        data:{phone:document.getElementsByName("phone")[0].value,cod:document.getElementsByName("phCod")[0].value},
        success:function (result) {
            if (result.code == 200) {
                //将token放入到本地存储中
                localStorage.setItem("token", result.data);
                window.location = '../html/index.html';
            }else if (result.code ==300){
                $(function () {
                    layer.alert('新用户首次登录，您的新人专享优惠券已发送至你的账户，请注意查收!', {
                        time: 5 * 1000,
                        success: function (layero, index) {
                            var timeNum = this.time / 1000, setText = function (start) {
                                layer.title('<span class="layui-font-red">' + (start ? timeNum : --timeNum) + '</span> 秒后自动关闭', index);
                            };
                            setText(!0);
                            this.timer = setInterval(setText, 1000);
                            if (timeNum <= 0) clearInterval(this.timer);
                        },
                        end: function () {
                            clearInterval(this.timer);
                            localStorage.setItem("token", result.data);
                            window.location = '../html/index.html';
                        }
                    });
                })
            }else if (result.code==400){
                layer.msg("验证码已过期，请重新发送！")
            }else if (result.code==500){
                layer.msg("验证码错误，请重新确认验证码！")
            }else if (result.code==505){
                layer.msg("请输入验证码")
            }
        }
    })
}